package com.example.konwRice.index.Util;


import android.content.Context;


import com.example.konwRice.R;
import com.example.konwRice.index.Bean.NewsBean;
import com.example.konwRice.index.Bean.RiceBean;
import com.example.konwRice.index.Dao.NewsDao;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;


public class DataUtil {
    private static boolean flag1=false;
    private static boolean flag2=false;
    private static final String[] RiceType={"大米","黑米","糯米","糙米","小米"};
    private static final String[] RiceImageUrl={"https://pic.ntimg.cn/file/20180808/26620058_192503947000_2.jpg",
            "https://bkimg.cdn.bcebos.com/pic/77094b36acaf2eddab1c131d8d1001e938019350?x-bce-process=image/resize,m_lfit,w_290,h_290,limit_1/format,f_auto",
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg4.99114.com%2Fgroup10%2FM00%2F3E%2FDA%2FrBADs1oRBsGAPGtoAAfdgC13oO0837.jpg&refer=http%3A%2F%2Fimg4.99114.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1641742141&t=9c61b4fd4cb848c6f98c9525820a26f3",
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fdpic.tiankong.com%2Fpj%2F99%2FQJ7101633459.jpg&refer=http%3A%2F%2Fdpic.tiankong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1641742223&t=a1616cacf6381833aff23269d897e28b",
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fg.search.alicdn.com%2Fimg%2Fbao%2Fuploaded%2Fi4%2Fi2%2F3980563872%2FO1CN01s2d7vu1eTQ8sox6QP_%21%213980563872.jpg&refer=http%3A%2F%2Fg.search.alicdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1641741726&t=784093e7a67f42aa42c866b05360ca26"};

    public static ArrayList<RiceBean> initRiceType(){
        ArrayList<RiceBean> riceBeans=new ArrayList<RiceBean>();
        for (int i=0;i<RiceType.length;i++){
            RiceBean riceBean=new RiceBean(RiceType[i],RiceImageUrl[i]);
            riceBeans.add(riceBean);
        }
        return riceBeans;
    }

    public static void getTeaData(String url,Context context) throws IOException {
        if(flag2){

        }
        /*
        绿茶
         */
        else {
            String Url="http://dmcyw.99114.com/supply/l_%E5%A4%A7%E7%B1%B3_12155_0_0_0_0_0_0_1.html";
            Document document= Jsoup.connect(Url).get();
            Elements TeaLists=document.select("div.t2038-apple-stock-left-conTop-detail");
            NewsDao newsDao=new NewsDao(context);
            for(Element Tea:TeaLists){
                String TeaUrl="http://dmcyw.99114.com/"+Tea.select("a").attr("href");
                String title=Tea.select("a").select("font").text();
                String time=Tea.select("a").text();
                String imageUrl="http://damicyd.99114.com/"+Tea.select("div.t2038-apple-stock-left-conTop-pic").select("a").select("img").attr("src");
                NewsBean newsBean=new NewsBean(title,time,TeaUrl,imageUrl);
                newsBean.setType("大米");
                newsDao.insertNews(newsBean);
            }
        /*
        青茶
         */
            Url="http://dmcyw.99114.com/supply/l_%E9%BB%91%E7%B1%B3_12155_0_0_0_0_0_0_1.html";
            document= Jsoup.connect(Url).get();
            TeaLists=document.select("div.t2038-apple-stock-left-conTop-detail");
            newsDao=new NewsDao(context);
            for(Element Tea:TeaLists){
                String TeaUrl="http://dmcyw.99114.com/"+Tea.select("a").attr("href");
                String title=Tea.select("a").select("font").text();
                String time=Tea.select("a").text();
                String imageUrl="http://damicyd.99114.com/"+Tea.select("div.t2038-apple-stock-left-conTop-pic").select("a").select("img").attr("src");
                NewsBean newsBean=new NewsBean(title,time,TeaUrl,imageUrl);
                newsBean.setType("黑米");
                newsDao.insertNews(newsBean);
            }
        /*
        黄茶
         */
            Url="http://www.chawh.net/hc/";
            document= Jsoup.connect(Url).get();
            TeaLists=document.select("div.pd2column");
            newsDao=new NewsDao(context);
            for(Element Tea:TeaLists){
                String TeaUrl="http://www.chawh.net"+Tea.select("dl").select("dd").select("strong").select("a").attr("href");
                String title=Tea.select("dl").select("dd").select("strong").select("a").text();
                String time=Tea.select("div.title").select("h2").select("a").text();
                String imageUrl="http://www.chawh.net"+Tea.select("dl").select("dt").select("a").select("img").attr("src");
                NewsBean newsBean=new NewsBean(title,time,TeaUrl,imageUrl);
                newsBean.setType("黄茶");
                newsDao.insertNews(newsBean);
            }
        /*
        红茶
         */
            Url="http://dmcyw.99114.com/supply/l_%E7%B3%99%E7%B1%B3_12155_0_0_0_128113103_43288:16016_0_1.html";
            document= Jsoup.connect(Url).get();
            TeaLists=document.select("div.pd2column");
            newsDao=new NewsDao(context);
            for(Element Tea:TeaLists){
                String TeaUrl="http://dmcyw.99114.com/"+Tea.select("a").attr("href");
                String title=Tea.select("div.t2038-apple-stock-left-conTop-detail").select("a").text();
                String time=Tea.select("div.t2038-apple-stock-left-conTop-detail").select("a").select("font").text();
                String imageUrl="http://damicyd.99114.com/"+Tea.select("div.t2038-apple-stock-left-conTop-detail").select("a").select("img").attr("src");
                NewsBean newsBean=new NewsBean(title,time,TeaUrl,imageUrl);
                newsBean.setType("糙米");
                newsDao.insertNews(newsBean);
            }
        /*
        花茶
         */
            Url="http://dmcyw.99114.com/supply/l_%E9%BB%84%E5%B0%8F%E7%B1%B3_12155_0_0_0_0_0_0_1.html";
            document= Jsoup.connect(Url).get();
            TeaLists=document.select("div.pd2column");
            newsDao=new NewsDao(context);
            for(Element Tea:TeaLists){
                String TeaUrl="http://dmcyw.99114.com/"+Tea.select("a").attr("href");
                String title=Tea.select("div.t2038-apple-stock-left-conTop-detail").select("a").text();
                String time=Tea.select("div.t2038-apple-stock-left-conTop-detail").select("a").select("font").text();
                String imageUrl="http://damicyd.99114.com/"+Tea.select("div.t2038-apple-stock-left-conTop-detail").select("a").select("img").attr("src");
                NewsBean newsBean=new NewsBean(title,time,TeaUrl,imageUrl);
                newsBean.setType("小米");
                newsDao.insertNews(newsBean);
            }
        }

    }

}
